/*
 * @Author: YWY
 * @Date: 2022-02-22 16:17:42
<<<<<<< HEAD
 * @LastEditors: lihao
 * @LastEditTime: 2022-03-07 00:23:39
=======
 * @LastEditors: YWY
 * @LastEditTime: 2022-03-06 23:42:30
>>>>>>> d3aa6a9659339b5d06754a19472d47be7a7aee80
 */
import { Tab, Toast } from 'vant'
import { cancel } from '../request.config.js'
import request from '@/api/request.config.js'
// import '@/api/request.js'
/**
 * @description: 登录接口
 * @param1 {*}
 * @return {*}
 * @detail: 
 */
export const login = (user_name, password) => {
  return request.post('/users/login', {
    user_name,
    password
  }).catch(rej => { console.log(rej) })
}
/**
 * @description: 登出接口
 * @param {*}
 * @return {*}
 */
export const logout = () => {
  localStorage.removeItem('Authorization')
}
/**
 * @description: 用户注册接口
 * @param {*}
 * @return {*}
 */
export const register = (user_name, password, sex, is_active) => {
  return request.post('/users/register', {
    user_name,
    password,
    sex,
    is_active
  }).catch(rej => { console.log(rej) })
}


/**
 * @description: 修改密码接口
 * @param1 {*}
 * @return {*}
 * @detail: 
 */
export const changePassword = (password, old) => {
  return request.patch('/users/password', {
    "password": password,
    "old": old
  })
}


/**
 * @description: 更新token的接口
 * @param1 {*}
 * @return {*}
 * @detail: 
 */
export const updateToken = () => {
  return request.get('/users/updatetoken')
}


/**
 * @description: 获取所有分区接口
 * @param {*}
 * @return {*}
 */

export const getSectionList = () => {
  return request.get('/partition/queryAllPartition')
}

/**
 * @description:获取分区文章接口 
 * @param {*}
 * @return {*}
 */
export const getSectionArticleList = (id, size, current) => {
  if (/section/.test(window.location.pathname)) {
    if (cancel !== null) {
      cancel();
      console.log('取消上一次获取文章列表请求')
    }
  }
  console.log("在axios内", id)
  return request.get(`/articles/public/page?partition_id=${id}&current=${current}&size=${size}`)
}

/**
 * @description: 搜索关键词联想接口
 * @param {*}
 * @return {*}
 */
export const getKeywordAssociation = (val) => {
  if (cancel !== null) {
    cancel();
    console.log('取消上一次获取搜获关键词列表请求')
  }
  return request.get(`/searchBaidu/sugrec?prod=pc&wd=${val}`)

  // http://localhost:3000/searchBaidu/sugrec?prod=pc&wd=${val}
}


/**
 * @description: 文章搜索请求
 * @param {*}
 * @return {*}
 */
export const getArtcileSearch = (val, current, size) => {
  return request.get(`/search/byword?wd=${val}&current=${current}&size=${size}`)
}

/**
 * @description: 用户搜索请求
 * @param {*}
 * @return {*}
 */
export const getUserSearch = (val) => {
  return request.get(`/search/byname?like_name=${val}`)
}

/**
 * @description: 上传头像接口
 * @param {*}
 * @return {*}
 */
export const uploadUserPic = (data) => {
  return request.post('/users/upload', data)
}

/**
 * @description: 获取用户发表文章接口
 * @param {*}
 * @return {*}
 */
export const getUserArticle = (id) => {
  return request.get("/articles/public/page", {
    params: {
      user_id: id
    }
  })
}

/**
 * @description: 获取用户收藏文章接口
 * @param {*}
 * @return {*}
 */
export const getUserFavorite = () => {
  return request.get("/collections/getlist");
}

/**
 * @description: 根据ID获取其他用户资料接口
 * @param {*}
 * @return {*}
 */
export const getUserData = (id) => {
  return request.get(`/users/searchbyid?id=${id}`)
}

/**
 * @description: 判断用户是否关注
 * @param {*}
 * @return {*}
 */
export const followJug = (id) => {
  return request.get(`/follows/ornot?follow_id=${id}`)
}
/**
 * @description: 关注用户
 * @param {*}
 * @return {*}
 */
export const followUser = (id) => {
  return request.post("/follows/addfollow", {
    "follow_id": id
  })
}

/**
 * @description: 取消关注
 * @param {*}
 * @return {*}
 */
export const disFollowUser = (id) => {
  return request.post("/follows/delfollow", {
    "follow_id": id
  })
}

/**
 * @description: 获取关注列表
 * @param {*}
 * @return {*}
 */
export const getUserFollows = (id) => {
  return request.get(`/follows/findfollow?user_id=${id}`)
}

/**
 * @description: 获取粉丝列表
 * @param {*}
 * @return {*}
 */
export const getFans = (id) => {
  return request.get(`/follows/allfollow?follow_id=${id}`)
}

/**
 * @description: 获取用户总点赞数
 * @param {*}
 * @return {*}
 */
export const getTotalLikes = (id) => {
  return request.get(`/likes/count?user_id=${id}`)
}

/**
 * @description: 上传资料接口
 * @param {*}
 * @return {*}
 */
export const uploadUserData = (userName, sex, city) => {
  return request.patch("/users/change", {
    "name": userName,
    "sex": sex,
    "city": city
  })
}

/**
 * @description: 获取自己发表文章接口
 * @param {*}
 * @return {*}
 */
export const getOwnArticle = () => {
  return request.get("/articles/own/page")
}